<template>
  <el-dialog
    title="新增员工"
    :visible.sync="isShow"
    width="50%"
    @close="closeEvent"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" placeholder="1~30个字符" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" placeholder="选择日期" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="1~20个字符" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <div v-outClick="bindClick">
          <el-input
            v-model="form.departmentName"
            placeholder="请选择"
            readonly
            @focus="focusEvent"
          />
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="treeClick"
          />
        </div>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="form.correctionTime" placeholder="选择日期" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center; margin: 10px 0">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { validateMobile } from '@/utils/validate.js'
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  data() {
    return {
      isShow: false,
      treeShow: false,
      treeData: [],
      employeesData,
      form: {
        username: '', // string	非必须		姓名
        mobile: '', // string	非必须		手机号
        timeOfEntry: '', // string	非必须		入职时间
        formOfEmployment: '', // number	非必须		聘用形式
        workNumber: '', // string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        correctionTime: '' // string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'blur' }],
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              validateMobile(value)
                ? callback()
                : callback(new Error('请输入正确手机号'))
            },
            trigger: 'blur'
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 部门聚焦显示组织架构
    async focusEvent() {
      this.treeShow = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        // console.log('部门列表', res)
        this.treeData = this.getTree(res.data.depts, '')
      }
    },
    // 获取树形结构
    getTree(arr, pid = '') {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.getTree(arr, item.id)
          return true
        }
      })
    },
    treeClick(data) {
      this.form.departmentName = data.name
      this.treeShow = false
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 验证成功,新增员工信息
          await sysUserPost(this.form)
          this.$message.success('新增员工成功!')
          this.closeEvent() // 关闭弹窗
          this.$parent.getData() // 调用父组件获取员工方法
        } else {
          console.log('验证失败!')
        }
      })
    },
    // 关闭
    closeEvent() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        username: '', // string	非必须		姓名
        mobile: '', // string	非必须		手机号
        timeOfEntry: '', // string	非必须		入职时间
        formOfEmployment: '', // number	非必须		聘用形式
        workNumber: '', // string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        correctionTime: '' // string	非必须		转正时间
      }
    },
    bindClick() {
      this.treeShow = false
    }
  }
}
</script>

<style></style>
